package com.company.bookstore.controller.book_module;


import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.BookCategory;
import com.company.bookstore.entities.book_module.Category;
import com.company.bookstore.service.book_module.BookCategoryService;
import com.company.bookstore.service.book_module.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-12
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Resource
    CategoryService categoryService;


    @PostMapping("/addCategory")
    public MvcResult<Object> addCategory(@RequestBody Category category) {
        Integer categoryId = categoryService.addCategory(category); //返回值为新增数据的id
        if (categoryId == null) {
            return MvcResult.ADD_FAIL;
        } else {
            return MvcResult.ADD_SUCCESS;
        }
    }

    @PostMapping("/updateCategory")
    public MvcResult<Object> updateCategory(@RequestBody Category category) {
        Integer affectRow = categoryService.updateCategory(category);
        if (affectRow == 0) {
            return MvcResult.UPDATE_FAIL;
        } else {
            return MvcResult.UPDATE_SUCCESS;
        }
    }

    @PostMapping("/deleteCategory/{cId}")
    public MvcResult<Object> deleteCategory(@PathVariable Integer cId) {
        Integer affectRow = categoryService.deleteCategory(cId);
        if (affectRow == 0) {
            return MvcResult.DELETE_FAIL;
        } else {
            return MvcResult.DELETE_SUCCESS;
        }
    }
}

